<?php
function get_title($header) {
	$pattern = "/<title>(.*?)<\/title>/ i";
	if (preg_match_all ( $pattern, $header, $matches )) {
		$title = trim ( $matches [1] [0] );
		$title = preg_replace ( "/Powered by(.*)/ i", "", $title );
		return $title;
	} 
	return FALSE ;
}

function get_a_tag($redirect_url,$content) {
	$url_parts = splitURL($redirect_url);
	$pattern = "/\<a\s.*?\>.*?\<\/a\>/ i";
	if (preg_match_all ( $pattern, $content, $matches )) {
		$a_tags = $matches [0];
		$A_count = count ( $a_tags );
		$url_list = array ();
		$filter = array() ;
		$image_urls =array() ;
		
		foreach ( $a_tags as $a_tag ) {
			//echo $a_tag.PHP_EOL ;
			$a_tag = str_replace ( "#a#b#c#d", "", $a_tag );
			
			$url_pattern = '/href="(.*?)"/ i';
			if (! preg_match_all ( $url_pattern, $a_tag, $match )) {
				$url_pattern = '/href=\'(.*?)\'/ i';
				if (! preg_match_all ( $url_pattern, $a_tag, $match )) {
					$url_pattern = '/href=([^>]*?)\s/ i';
					if (! preg_match_all ( $url_pattern, $a_tag, $match )) {
						$url_pattern = '#href=(.*?)(\'|"|>|<)# i';
						preg_match_all ( $url_pattern, $a_tag, $match );
					}
				}
			}
			
			if ($match && ! empty ( $match [0] )) {
				$relative_url = trim ( $match [1] [0] );
				$relative_url = str_replace ( '"', "", $relative_url );
				$relative_url = str_replace ( '\'', "", $relative_url );
				
				if (stripos ( $relative_url, "http://http:" ) > - 1) {
					$log_name = "error_url_http";
					$msg = $a_tag . "\n" . $relative_url;
					///admin_log ( $msg, $log_name );
					continue;
				}
						
				$url_info = change_url_from_relative_to_absolute ( $url_parts, $relative_url );
				if(stripos($url_info['link'], "jpg"))
				{
					// echo $a_tag.PHP_EOL ;
				}
				
				if (!$url_info || empty ( $url_info )) {
					continue;
				}
			}
			else 
			{
				// echo $a_tag.PHP_EOL ;
				continue ;
			}
			
			$AnchorText="";
			// 获取锚文本 锚文本有可能是title或者是alt值 这个值是最精准的选择
			$title_pattern = '/title="(.*?)"/ i';
			if (! preg_match_all ( $title_pattern, $a_tag, $match )) {
				$title_pattern = '/title=\'(.*?)\'/ i';
				if (! preg_match_all ( $title_pattern, $a_tag, $match )) {
					$title_pattern = '/title=([^>]*?)\s/ i';
					if (! preg_match_all ( $title_pattern, $a_tag, $match )) {
						$title_pattern = '#title=(.*?)(\'|"|>|<)# i';
						preg_match_all ( $title_pattern, $a_tag, $match );
					}
				}
			}
						
			if ($match && ! empty ( $match [0] )) {
				$AnchorText = trim ( $match [1] [0] );
			}
			
			if(empty($AnchorText))
			{
				$alt_pattern = '/alt="(.*?)"/ i';
				if (! preg_match_all ( $alt_pattern, $a_tag, $match )) {
					$alt_pattern = '/alt=\'(.*?)\'/ i';
					if (! preg_match_all ( $alt_pattern, $a_tag, $match )) {
						$alt_pattern = '/alt=([^>]*?)\s/ i';
						if (! preg_match_all ( $alt_pattern, $a_tag, $match )) {
							$alt_pattern = '#alt=(.*?)(\'|"|>|<)# i';
							preg_match_all ( $alt_pattern, $a_tag, $match );
						}
					}
				}
				
				if ($match && ! empty ( $match [0] )) {
					$AnchorText = trim ( $match [1] [0] );
					// echo $AnchorText.PHP_EOL ;
				}				
			}
			
			if(empty($AnchorText))
			{
				$pattern = "/<.*?>/";
				$AnchorText = trim(preg_replace($pattern, "", $a_tag)) ;
			}
						
			if(empty($AnchorText))
			{
				//$url_info['a_tag'] = $a_tag ;
				$image_urls[] = $url_info ;
				continue ;
			}
			
			if(!in_array($url_info['link'], $filter))
			{
				$url_info['AnchorText'] = $AnchorText ;
				$url_list [] = $url_info;
				$filter[] = $url_info['link'] ;
			}
		}
		
		foreach ($image_urls as $image_url)
		{
			if(!in_array($image_url['link'], $filter))
			{
				$image_url['AnchorText'] = "" ;
				$url_list [] = $image_url;
				$filter[] = $image_url['link'] ;
				//echo "hellohello".PHP_EOL ;
				//echo $image_url['a_tag'].PHP_EOL ;
			}
		}
			
		$page_info_url ['url_list'] = $url_list;
		$page_info_url ['a_count'] = $A_count;
		return $page_info_url;
	}
	
	return FALSE;
}